home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / Amigamain / Example3 / amigamain.c next >
C/C++ Source or Header  |  2004-06-26  |  4KB  |  207 lines

  1. #include <proto/exec.h>
  2. #include <proto/dos.h>
  3. #include <proto/intuition.h>
  4. #include <proto/locale.h>
  5. #define CATCOMP_NUMBERS
  6. #define CATCOMP_CODE
  7. #define CATCOMP_BLOCK
  8. #include "app_strings.h"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include "amigamain.h"
  13. #include "app.h"
  14.  
  15. struct Config config;
  16. char *vers="\0$VER: MyApp 0.1 (1.1.2004)";
  17. static BPTR oldout, newout;
  18.  
  19. struct IntuitionBase *IntuitionBase = NULL;
  20. struct LocaleBase *LocaleBase = NULL;
  21. struct LocaleInfo li;  /* extern */
  22.  
  23.  
  24. static void cleanexit( void );
  25. static struct Library *save_open_library( UBYTE *libName, ULONG version);
  26.  
  27. int
  28. main(int argc, char *argv[] )
  29. {
  30.     atexit( cleanexit );
  31.     if (LocaleBase = (struct LocaleBase*) OpenLibrary("locale.library",38))
  32.     {
  33.         li.li_LocaleBase = LocaleBase;
  34.         li.li_Catalog    = OpenCatalog(NULL , "app.catalog" ,
  35.             OC_BuiltInLanguage , (ULONG)"english" , TAG_DONE );
  36.     }
  37.     config.message_request = 1;
  38.     config.message_output = 1;
  39.     config.nr = 5;
  40.     if ( argc == 0 )
  41.     {
  42.         /* started from Workbench */
  43.         config.start_from_wb = TRUE;
  44.         if ( ! freopen( "con:10/50/300/100/stdin/AUTO"  , "r", stdin  ))
  45.         {
  46.             messagef_loc( MSG_ERR_OPEN, "'stdin'" );
  47.             exit( EXIT_FAILURE );
  48.         }
  49.         if ( ! freopen( "con:10/150/300/100/stdout/AUTO", "w", stdout ))
  50.         {
  51.             messagef_loc( MSG_ERR_OPEN, "'stdout'" );
  52.             exit( EXIT_FAILURE );
  53.         }
  54.  
  55.         newout = Open("con:10/250/300/100/AmigaDos/AUTO", MODE_OLDFILE);
  56.         if ( ! newout )
  57.         {
  58.             messagef_loc( MSG_ERR_OPEN, "'AmigaDos'" );
  59.             exit( EXIT_FAILURE );
  60.         }
  61.         oldout = SelectOutput(newout );
  62.     }
  63.     IntuitionBase = (struct IntuitionBase *)
  64.         save_open_library("intuition.library", 40);
  65.     config.all_libraries_open = TRUE;
  66.     if ( config.start_from_wb )
  67.     {
  68.     }
  69.     else
  70.     {
  71.         messagef_loc( MSG_ERR_SHELL );
  72.         exit( EXIT_FAILURE );
  73.     }
  74.     APP_run();
  75.     return EXIT_SUCCESS;
  76. }
  77.  
  78. /*
  79.   open EasyRequest
  80. */
  81. LONG
  82. show_request( char *title, char *text, char *button, ... )
  83. {
  84.     va_list ap;
  85.     va_start(ap, button );
  86.     return show_request_args(title, text, button, ap);
  87.     va_end(ap );
  88. }
  89.  
  90. /*
  91.   open EasyRequest (va_list)
  92. */
  93. LONG
  94. show_request_args( char *title, char *text, char *button, va_list ap )
  95. {
  96.     struct EasyStruct es = {sizeof (struct EasyStruct), 0, 0, 0, 0};
  97.     es.es_Title = title;
  98.     es.es_TextFormat = text;
  99.     es.es_GadgetFormat = button;
  100.     return EasyRequestArgs(config.reqwin, &es, 0, ap);
  101. }
  102.  
  103. /*
  104.   formated output to Output() and/or Workbench
  105.   parameters like vprintf
  106. */
  107. void
  108. vmessagef( char *format, va_list ap )
  109. {
  110.     if ( ! format) return;
  111.     if ( config.message_output )
  112.     {
  113.         VPrintf(format, ap);
  114.         Flush( Output() );
  115.     }
  116.     if ( config.message_request && IntuitionBase )
  117.     {
  118.         show_request_args(
  119.             GetString(&li, MSG_REQTITLE), format,
  120.             GetString(&li , MSG_OK), ap);
  121.     }
  122. }
  123.  
  124. /*
  125.   messagef (like printf)
  126. */
  127. void
  128. messagef( char *format, ... )
  129. {
  130.     va_list ap;
  131.     va_start(ap, format);
  132.     vmessagef(format, ap);
  133.     va_end(ap);
  134. }
  135.  
  136. /*
  137.   message_f with locale support
  138. */
  139. void
  140. messagef_loc( LONG msg_id, ... )
  141. {
  142.     va_list ap;
  143.     char *format;
  144.     format = GetString(&li , msg_id);
  145.     va_start(ap, msg_id);
  146.     vmessagef(format, ap);
  147.     va_end(ap);
  148. }
  149.  
  150. /*
  151.   copies string s to new allocated RAM
  152. */
  153. char *
  154. strcpy_malloc(const char *s )
  155. {
  156.     char *dest;
  157.     if ( ! s)
  158.         return NULL;
  159.     dest = malloc( strlen( s ) + 1);
  160.     if ( ! dest )
  161.     {
  162.         messagef_loc( MSG_ERR_RAM , "strcpy_malloc");
  163.         exit( EXIT_FAILURE );
  164.     }
  165.     strcpy(dest, s);
  166.     return dest;
  167. }
  168.  
  169. /*
  170.   OpenLibrary with check of return value
  171. */
  172. static struct Library *
  173. save_open_library(UBYTE *libName, ULONG version)
  174. {
  175.     struct Library *lib;
  176.     lib = OpenLibrary(libName, version);
  177.     if ( ! lib )
  178.     {
  179.         messagef_loc( MSG_ERR_LIB ,libName, version );
  180.         exit( EXIT_FAILURE);
  181.     }
  182.     return lib;
  183. }
  184.  
  185.  
  186. /*
  187.   cleanup function for atexit
  188. */
  189. static void
  190. cleanexit( void )
  191. {
  192.     APP_clean();
  193.     if ( config.start_from_wb )
  194.         Delay( 200 );
  195.     if ( config.all_libraries_open )
  196.     {
  197.         CloseCatalog(li.li_Catalog);
  198.         if ( newout )
  199.         {
  200.             SelectOutput(oldout );
  201.             Close(newout );
  202.         }
  203.     }
  204.     CloseLibrary((struct Library *) IntuitionBase );
  205.     CloseLibrary((struct Library*)LocaleBase);
  206. }
  207.